package com.lcx.plugin.neteaseCloud.music;


import com.alibaba.fastjson.JSONArray;
import com.intellij.openapi.ui.Messages;
import com.lcx.plugin.neteaseCloud.component.handle.UserHandle;
import com.lcx.plugin.neteaseCloud.music.api.MusicListApi;
import com.lcx.plugin.neteaseCloud.music.api.UserApi;

import java.util.Objects;

/**
 * @author daxiong
 * @date 2021/9/9 下午4:04
 */
public class UserInfo {

    public static void loginByEmail() {
        int code = UserApi.loginByEmail(DataCenter.accountNumber, DataCenter.accountPassword);
        System.out.println("profile: " + DataCenter.userProfile);
        checkLogin(code);
        // 登录后的数据操作
        afterLogin();
    }

    public static void loginByPhoneNumber() {
        int code = UserApi.loginByPhoneNumber(DataCenter.accountNumber, DataCenter.accountPassword);
        System.out.println("profile: " + DataCenter.userProfile);
        checkLogin(code);
        // 登录后的数据操作
        afterLogin();
    }

    public static void loginByPhoneCode() {
        int code = UserApi.loginByPhoneCode(DataCenter.accountNumber, DataCenter.phoneCode);
        System.out.println("profile: " + DataCenter.userProfile);
        checkLogin(code);
        // 登录后的数据操作
        afterLogin();
    }

    public static void loginByQrCode() {
        DataCenter.userProfile = UserApi.getProfile();
        System.out.println("profile: " + DataCenter.userProfile);
        // 登录后的数据操作
        afterLogin();
    }

    /**
     * 校验登录是否成功
     *
     * @param code
     * @return void
     * @author daxiong
     * @date 2021/10/20 下午4:13
     */
    private static void checkLogin(int code) {
        if (!Objects.equals(code, 200)) {
            Messages.showMessageDialog("登录失败", "提示", Messages.getInformationIcon());
        }
    }

    /**
     * 发送手机验证码
     *
     * @param phone
     * @return void
     * @author daxiong
     * @date 2021/10/10 上午11:12
     */
    public static void sendPhoneCode(String phone) {
        UserApi.sendPhoneCode(phone);
    }

    /**
     * 登录后的操作
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/16 下午1:54
     */
    private static void afterLogin() {
        // 设置用户信息到数据中心
        DataCenter.uid = DataCenter.userProfile.getString("userId");
        DataCenter.cookie = DataCenter.userInfo.getString("cookie");
        System.out.println("DataCenter.cookie = " + DataCenter.cookie);
        DataCenter.userProfile = UserApi.getProfile();
        // 获取用户歌单
        JSONArray musicList = MusicListApi.getMusicList();
        DataCenter.musicList = musicList;
        System.out.println("musicList = " + musicList);
    }

    /**
     * 登出
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 下午1:19
     */
    public static void logout() {
        if (Objects.nonNull(DataCenter.cookie)) {
            UserApi.logout();
            UserHandle.deleteLocalFile();
        }
        DataCenter.uid = null;
        DataCenter.cookie = null;
        DataCenter.userProfile = null;
        DataCenter.musicList = null;
        DataCenter.queueList.clear();
        DataCenter.playingSongEntity = null;
        DataCenter.playMode = 0;
        DataCenter.accountNumber = null;
        DataCenter.accountPassword = null;
        DataCenter.loginType = null;
        DataCenter.userInfo = null;
        DataCenter.mainPanel.clearMyPanel();
        DataCenter.mainPanel.clearMusicPanel();
        DataCenter.mainPanel.clearQueuePanel();
        DataCenter.mainPanel.clearPlayPanel();
    }
}
